package demo.bean;

import java.time.LocalDateTime;
import java.util.Random;

import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;


@Component 
public class TaskBean {
	// 定时任务 
	
	private static final Random rand = new Random();
	
	@Scheduled(cron="* * * * * ?")
	public void task1(){	
	    // 模拟执行抛异常	
		if(rand.nextInt(10)%3 ==0){
			throw new RuntimeException("TaskBean.task1() run error,"+LocalDateTime.now());
		}
		
	    System.out.println(Thread.currentThread().getName() + " TaskBean.task1() run " + LocalDateTime.now());
	    try{
	        Thread.sleep(2000);
	    }catch(Throwable e){
	    	System.out.println("sleep error,"+e);
	    }
	}
	
	@Scheduled(fixedDelayString = "2000")
	  public void task2() {
	    System.out.println(Thread.currentThread().getName() + " TaskBean.task2() run " + LocalDateTime.now());
	  }

}
